<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
    <title>社交登陆测试页</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<body>
<div>点击如下按钮，授权登录</div>
<div>
    手机号<input id="mobile" value="15601691300"><br>
    手机验证码<input id="smsCode">
    <button id="send_sms_code">发送手机验证码</button>
    <br>
    <button id="wx_pub">微信公众号授权登录</button>
</div>
</body>
<script>
    // let server = 'http://127.0.0.1:48080';
    let server = 'http://192.168.1.2:48080';

    let type = 31; //登录类型 微信公众号

    // 微信公众号
    $("#wx_pub").on("click", function () {
        let code = getUrlParam("code"); // 访问授权连接后，会回调本页面地址，参数在本页面url后面
        let state = getUrlParam("state");
        console.log("获取code: " + code + ", state: " + state)

        let data = {
            'mobile': $('#mobile').val(),
            'smsCode': $('#smsCode').val(),
            'code': code,
            'state': state,
            'type': type
        }

        // 调用授权登录接口
        $.ajax({
            url: server + "/app-api/social-login2",
            method: 'POST',
            data: JSON.stringify(data),
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function( result ) {
                if (result.code !== 0) {
                    alert('调用授权登录接口失败，原因：' + result.msg)
                    return;
                }
                alert("授权登录成功, token: "+result.data.token)
            }
        })
    });

    // 发送手机验证码
    $("#send_sms_code").on("click", function () {
        let data = {
            'mobile': $('#mobile').val(),
            'scene': 1 // 手机号登陆 类型
        }
        $.ajax({
            url: server + "/app-api/send-sms-code",
            method: 'POST',
            data: JSON.stringify(data),
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (result) {
                if (result.code !== 0) {
                    alert('发送手机验证码失败，原因：' + result.msg)
                    return;
                }
                alert("发送成功, 请查看日志");
            }
        })
    })


    //获取url中的参数
    function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }
</script>
</html>
